// pages/my_zong/my_zong.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    array: ['微信', '线下'],
    index: "",
    ifxian: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var zid = options.zid
    var docmoney = options.docmoney
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    that.setData({
      docmoney: docmoney,
      zid: zid
    })
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var that = this
    var ifxian = that.data.ifxian
    if (e.detail.value == '1') {
      that.setData({
        ifxian: true,
      })
    } else {
      that.setData({
        ifxian: false,
      })
    }
    that.setData({
      index: e.detail.value
    })
  },
  //明细
  mingClick: function() {
    var openid = wx.getStorageSync('openid')
    wx.navigateTo({
      url: '/hyb_yl/usermx/usermx?openid=' + openid,
    })
  },
  //须知
  xuzhi: function() {
    wx.navigateTo({
      url: '/hyb_yl/xuzhi/xuzhi',
    })
  },
  //设置最大提现金额
  money: function(e) {
    var that = this
    var money = that.data.money
    //console.log(e)
    if (e.detail.value >= money) {
      var value = money
      that.setData({
        value: value
      })
    }
  },
  //提交
  subClick: function(e) {
    var that = this;
    var base = that.data.base
    var zdtx = parseInt(base.zdtx)
    var txsx = base.txsx
    var zid = that.data.zid
    var formobj = e.detail.value;
    formobj.openid = wx.getStorageSync('openid');
    var tx_cost = parseFloat(formobj.tx_cost)
    var sj_cost = parseFloat(formobj.sj_cost)
    var zztx = (tx_cost - (tx_cost * (txsx / 100))).toFixed(2)
    var sxf = txsx + '%'
    var sy_money = parseFloat(sj_cost - tx_cost)
    let countmoney = that.data.countmoney
    var ifxian = that.data.ifxian
    console.log(sy_money)
    if (e.detail.value.sj_cost.length == 0) {
      wx.showModal({
        content: '您的账户暂无金额',
      })
    } else if (e.detail.value.tx_cost.length == 0) {
      wx.showModal({
        content: '请输提现金额',
      })
    } else if (e.detail.value.tx_type == '' || e.detail.value.tx_type == null) {
      wx.showModal({
        content: '请选择提现方式',
      })
    } else if (tx_cost > sj_cost) {
      wx.showModal({
        content: '超出实际金额',
      })
    } else if (tx_cost < zdtx) {
      wx.showModal({
        content: '提现金额必须大于等于' + zdtx,
      })
    } else if (ifxian == '1' && e.detail.value.tx_admin.length == 0) {
      wx.showModal({
        content: '请输入联系方式',
      })
    } else {
      wx.showModal({
        content: ' 实际到帐' + zztx + '扣除' + sxf + '手续费',
        success: function(res) {
          if (res.confirm) {
            console.log(res.confirm)
            console.log('用户点击确定');
            var tx_cost = e.detail.value.tx_cost;
            var tx_type = e.detail.value.tx_type;
            var tx_admin = e.detail.value.tx_admin
            var openid = wx.getStorageSync('openid');
            app.util.request({
              url: 'entry/wxapp/SaveTx',
              data: {
                sj_cost: zztx,
                tx_cost: tx_cost,
                tx_type: tx_type,
                openid: openid,
                tx_admin: tx_admin,
                zjid: zid,
                sy_money: sy_money
              },
              header: {
                'Content-Type': 'application/json'
              },
              success: function(res) {
                console.log(res)

                if (res.data.code == 1) {
                  wx.showToast({
                    title: '提交失败',
                  })
                } else {
                  wx.showToast({
                    title: '提交成功',
                  })
                  var pages = getCurrentPages();
                  var prevPage = pages[pages.length - 2];
                  prevPage.setData({
                    state: 1
                  })
                  wx.reLaunch({
                    url: '/hyb_yl/zhuanjiasubpages/pages/backstageMy/backstageMy',
                  })
                }

              },

              fail: function(err) {
                console.log(err);
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      });
      //获取用户信息
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getBase()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getBase: function() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/Base',
      success: function(res) {
        console.log(res)
        that.setData({
          base: res.data.data
        })
      },
      fail: function(err) {
        //console.log(err)
      },
    })
  },
})